package com.ncp.model.biz.registerlogin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.ncp.model.entity.registerlogin.*;
import com.ncp.model.dao.DbConnection;

public class UserManager {
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	private int pageCount=0;
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	private int rowCount=0;
	//根据id删除用户
	public boolean delUser(String id){
		Boolean b=false;
		try{
			DbConnection cd=new DbConnection();
			ct=cd.getConnByDataSource();
			String sql="delete from USERBASIC where UserID='"+id+"'";
			ps=ct.prepareStatement(sql);
			int num=ps.executeUpdate();
			if(num==1){
				//删除成功！
				b=true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(ps!=null){ps.close();}
				if(ct!=null){ct.close();}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return b;
	}
	//给用户分页
	public ArrayList fenYe(int pageSize,int pageNow){
		ArrayList al=new ArrayList();
  		try {
  			DbConnection cd=new DbConnection();
  			Connection ct=cd.getConnByDataSource();
  			ps=ct.prepareStatement("select count(*) from USERBASIC");
			rs=ps.executeQuery();
			if(rs.next()){
				rowCount=rs.getInt(1);	
			}
			//计算pageCount
			if(rowCount%pageSize==0){
				pageCount=rowCount/pageSize;
			}else{
				pageCount=rowCount/pageSize+1;
			}
  			if(rs.next()){
  				rowCount=rs.getInt(1);
  			}
  			if(rowCount%pageSize==0){
  				pageCount=rowCount/pageSize;
  			}else{
  				pageCount=rowCount/pageSize+1;
  			}
  			ps=ct.prepareStatement("select * from USERBASIC LIMIT "+(pageNow-1)*pageSize+","+pageNow*pageSize);
  			rs=ps.executeQuery();
  			while(rs.next()){
				UserBasicBean ubb=new UserBasicBean();
				ubb.setUserID(rs.getInt(1));
				ubb.setUserLoginName(rs.getString(2));
				ubb.setPassWord(rs.getString(3));
				ubb.setLevelID(rs.getInt(4));
				ubb.setUserName(rs.getString(5));
				ubb.setEmail(rs.getString(6));
				ubb.setTel(rs.getString(7));
				ubb.setAddress(rs.getString(8));
				ubb.setRegisterDate(rs.getString(9));
				ubb.setStatus(rs.getString(10));
				al.add(ubb);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	//修改用户信息
	public boolean updateUser(String id,String level,String status){
		boolean b=false;
		try{
			DbConnection cd=new DbConnection();
			ct=cd.getConnByDataSource();
			String sql="update USERBASIC set Status='"+status+"' where UserID='"+id+"'";
			ps=ct.prepareStatement(sql);
			int num=ps.executeUpdate();
			if(num==1){
				//删除成功！
				b=true;
			}
		}catch(Exception e){
			e.printStackTrace();	
		}finally{
			try{
			if(ps!=null){ps.close();}
			if(ct!=null){ct.close();}
		}catch(Exception e){
			e.printStackTrace();
		}
		}
		return b;
	}
	//关闭资源
	public void closeResource(){
		try {
			if(rs!=null){
				rs.close();
				rs=null;
			}
			if(ps!=null){
				ps.close();
				ps=null;
			}
			if(ct!=null){
				ct.close();
				ct=null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
